﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.UI;
using System.Web.UI.WebControls;
using WebDS;
using WebUS;
using IP.Core.IPCommon;
using WebDS.CDBNames;
using IP.Core.IPData;
using IP.Core.IPUserService;

public partial class DanhMuc_Danh_muc_thiet_bi : System.Web.UI.Page
{
    #region Members
    US_DM_THIET_BI m_us_dm_thiet_bi = new US_DM_THIET_BI();
    DS_DM_THIET_BI m_ds_dm_thiet_bi = new DS_DM_THIET_BI();
    #endregion

    #region PrivateMethods
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
                load_data_2_grid();
                load_cbo_loai_thiet_bi();
        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
    }

    private void load_cbo_loai_thiet_bi()
    {
        try
        {
            US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN();
            DS_CM_DM_TU_DIEN v_ds_cm_tu_dien = new DS_CM_DM_TU_DIEN();

            v_us_tu_dien.FillDataset(v_ds_cm_tu_dien, " WHERE ID_LOAI_TU_DIEN = " + (int)e_loai_tu_dien.LOAI_THIET_BI);

            m_cbo_loai_thiet_bi.DataSource = v_ds_cm_tu_dien.CM_DM_TU_DIEN;
            m_cbo_loai_thiet_bi.DataTextField = CM_DM_TU_DIEN.TEN_NGAN;
            m_cbo_loai_thiet_bi.DataValueField = CM_DM_TU_DIEN.ID_LOAI_TU_DIEN;
            m_cbo_loai_thiet_bi.DataBind();
            
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }

    private void load_data_2_grid() {
        try
        {

            m_us_dm_thiet_bi.FillDataset(m_ds_dm_thiet_bi);
            m_grv_dm_thiet_bi.DataSource = m_ds_dm_thiet_bi.DM_THIET_BI;
            m_grv_dm_thiet_bi.DataBind();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }

    }


   

    private void load_update_dm_thiet_bi(int i_int_row_index)
    {
        try
        {
            decimal v_dc_id_dm_thiet_bi = CIPConvert.ToDecimal(m_grv_dm_thiet_bi.DataKeys[i_int_row_index].Value);
            US_DM_THIET_BI v_us_dm_thiet_bi = new US_DM_THIET_BI(v_dc_id_dm_thiet_bi);
            m_lbl_thong_bao.ToolTip=CIPConvert.ToStr(v_dc_id_dm_thiet_bi);
            //m_hdf_id_dm_tu_dien.Value = CIPConvert.ToStr(v_dc_id_dm_thiet_bi);
            us_object_2_form(v_us_dm_thiet_bi);
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }


    
    private void us_object_2_form(US_DM_THIET_BI i_us) {
        m_cbo_loai_thiet_bi.SelectedValue=CIPConvert.ToStr(i_us.dcID_LOAI_THIET_BI);
        m_txt_ten_may.Text=i_us.strTEN_MAY;
        m_txt_model.Text=i_us.strMODEL;
        m_txt_hang_san_xuat.Text=i_us.strHANG_SAN_XUAT;
        m_txt_xuat_xu.Text=i_us.strXUAT_XU;
        m_txt_mo_ta_them.Text=i_us.strMO_TA_THEM;
        m_txt_hoa_chat_kem_theo.Text=i_us.strHOA_CHAT_KEM_THEO;
    }

    private void form_2_us_object()
    {
        m_us_dm_thiet_bi.dcID_LOAI_THIET_BI = CIPConvert.ToDecimal(m_cbo_loai_thiet_bi.SelectedValue);
        m_us_dm_thiet_bi.strTEN_MAY = m_txt_ten_may.Text.Trim();
        m_us_dm_thiet_bi.strMODEL = m_txt_model.Text.Trim();
        m_us_dm_thiet_bi.strHANG_SAN_XUAT = m_txt_hang_san_xuat.Text.Trim();
        m_us_dm_thiet_bi.strXUAT_XU = m_txt_xuat_xu.Text.Trim();
        m_us_dm_thiet_bi.strMO_TA_THEM = m_txt_mo_ta_them.Text.Trim();
        m_us_dm_thiet_bi.strHOA_CHAT_KEM_THEO = m_txt_hoa_chat_kem_theo.Text.Trim();
    }

    
    private void clear_dm_thiet_bi()
    {
        try
        {
            m_grv_dm_thiet_bi.EditIndex = -1;
            m_cbo_loai_thiet_bi.SelectedValue = null;
           
            m_txt_ten_may.Text = "";
            m_txt_model.Text = "";
            m_txt_hang_san_xuat.Text = "";
            m_txt_mo_ta_them.Text = "";
            m_txt_xuat_xu.Text = "";
            m_txt_hoa_chat_kem_theo.Text = "";

        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }

    private void delete_dm_thiet_bi(decimal i_id)
    {
        try
        {
            m_us_dm_thiet_bi.DeleteByID(i_id);
            load_data_2_grid();
            clear_dm_thiet_bi();
            m_lbl_thong_bao.Text = "Dữ liệu đã được xoá thành công!";
        }
        catch (System.Exception ex)
        {
            m_lbl_thong_bao.Text = "Đã xảy ra lỗi trong quá trình xoá!";
            CSystemLog_301.ExceptionHandle(this, ex);
        }
    }
    private void insert_dm_thiet_bi()
    {
        try
        {
            m_grv_dm_thiet_bi.EditIndex = -1;
            if (Page.IsValid)
            {
               
                
                form_2_us_object();
                m_us_dm_thiet_bi.Insert();
                m_lbl_thong_bao.Text = "Đã thêm mới thành công.";
                clear_dm_thiet_bi();
                load_data_2_grid();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_thong_bao.Text = "Lỗi trong quá trình thêm mới bản ghi.";
            throw v_e;
        }
    }
    private void update_dm_thiet_bi()
    {
        try
        {
            
            if (Page.IsValid)
            {
                
                if  (m_lbl_thong_bao.ToolTip=="")
                { m_lbl_thong_bao.Text = "Bạn phải chọn Thiết bị cần cập nhật."; return; }
                
                form_2_us_object();
               // m_us_dm_thiet_bi.dcID = CIPConvert.ToDecimal(this.m_hdf_id_dm_thiet_bi.Value);
                m_us_dm_thiet_bi.dcID = CIPConvert.ToDecimal(this.m_lbl_thong_bao.ToolTip);
                m_us_dm_thiet_bi.Update();
                m_lbl_thong_bao.Text = "Đã cập nhật bản ghi thành công.";
                clear_dm_thiet_bi();
                
                load_data_2_grid();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_thong_bao.Text = "Lỗi trong quá trình cập nhật bản ghi.";
            throw v_e;
        }
    }
    #endregion



    
    protected void m_cmd_xoa_trang_Click(object sender, EventArgs e)
    {
        try
        {
            clear_dm_thiet_bi();
        }
        catch (Exception v_e)
        {
           
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cmd_them_moi_Click(object sender, EventArgs e)
    {
        try
        {
            m_lbl_thong_bao.Text = "";
            insert_dm_thiet_bi();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    
    protected void m_grv_dm_thiet_bi_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            m_lbl_thong_bao.Text = "";
            load_update_dm_thiet_bi(e.RowIndex);
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    protected void m_cmd_cap_nhat_Click1(object sender, EventArgs e)
    {
        try
        {
            m_lbl_thong_bao.Text = "";
            update_dm_thiet_bi();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    protected void m_grv_dm_thiet_bi_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            decimal v_id_dm_thiet_bi = CIPConvert.ToDecimal(m_grv_dm_thiet_bi.DataKeys[e.RowIndex].Value);
            delete_dm_thiet_bi(v_id_dm_thiet_bi);
            load_data_2_grid();
            clear_dm_thiet_bi();
        }

        catch (System.Exception ex)
        {
            m_lbl_thong_bao.Text = "Đã xãy ra lỗi trong quá trình xoá!";
            CSystemLog_301.ExceptionHandle(this, ex);
        	
        }
    }

}